package com.yict.common.util;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;

public class ObjectUtil {
	public final static String DATE_FORMAT_TEXT = "yyyy-MM-dd HH:mm:ss.SSS";
	public final static String DATE_FORMAT_SHORT = "yyyy-MM-dd";
	
	/**
	 * 
	 * @param s
	 * @return
	 */
	public static Date s2d(String s){
		Date d = null;
		SimpleDateFormat sdf = null;
		try {
			if(s.matches("\\d{4}-\\d{2}-\\d{2}"))
				sdf = new SimpleDateFormat(DATE_FORMAT_SHORT);
			else if(s.matches("\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}\\.\\d{3}"))
				sdf = new SimpleDateFormat(DATE_FORMAT_TEXT);
			else{
				sdf = new SimpleDateFormat("EEE MMM dd yyyy hh:mm:ss Z");
			}
			d = sdf.parse(s);
		} catch (Exception e) {
		}
		return d;
	}
	
	/**
	 * 
	 * @param d
	 * @return
	 */
	public static String d2s(Date d,String format){
		String s = null;
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		try {
			s = sdf.format(d);
		} catch (Exception e) {
		}
		return s;
	}
	
	/**
	 * 
	 * @param s
	 * @return
	 */
	public static Date s2d(String s,String format){
		Date d = null;
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		try {
			d = sdf.parse(s);
		} catch (Exception e) {
		}
		return d;
	}
	
	/**
	 * 
	 * @param d
	 * @return
	 */
	public static String d2s(Date d){
		String s = null;
		SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_TEXT);
		try {
			s = sdf.format(d);
		} catch (Exception e) {
		}
		return s;
	}
	
	/**
	 * 
	 * @param d
	 * @return
	 */
	public static String o2s(Object o){
		String s = "";
		try {
			if(o == null) s = "";
			else s = String.valueOf(o);
		} catch (Exception e) {
			// TODO: handle exception
		}
		return s;
	}
	
	public static int s2i(String s){
		int i = 0;
		try {
			BigDecimal  num = new BigDecimal(s).setScale(BigDecimal.ROUND_HALF_DOWN);
			i = num.intValue();
		} catch (Exception e) {
			// TODO: handle exception
		}
		return i;
	}
	
	public static long s2l(String s){
		long l = 0;
		try {
			BigDecimal  num = new BigDecimal(s).setScale(BigDecimal.ROUND_HALF_DOWN);
			l = num.longValue();
		} catch (Exception e) {
			// TODO: handle exception
		}
		return l;
	}
	
	public static BigDecimal s2b(String s){
		BigDecimal b = new BigDecimal(0);
		try {
			b = new BigDecimal(s);
		} catch (Exception e) {
			// TODO: handle exception
		}
		return b;
	}
	
	/**
	 * 
	 * @param o
	 * @param df
	 * @return
	 */
	public static Long nvlLong(Long o,Long df){
		if(o == null) return df;
		return o;
	}
}
